//
//  AddressListCell.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/25.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "AddressListCell.h"

@interface AddressListCell ()
@property (nonatomic, strong) UILabel *nameLabel;          //收货人姓名
@property (nonatomic, strong) UILabel *phoneLabel;         //收货人联系方式
@property (nonatomic, strong) UILabel *addressLabel;       //收货人地址
@property (nonatomic, strong) UIButton *defaultButton;     //设为默认按钮
@property (nonatomic, strong) UIButton *editButton;         //编辑按钮
@property (nonatomic, strong) UIButton *deleteButton;      //删除按钮
@end

@implementation AddressListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    //姓名
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kAppWidth_Mini(50));
        make.top.mas_equalTo(kAppWidth_Mini(40));
        make.width.mas_equalTo(kAppWidth_Mini(200));
        make.height.mas_equalTo(kAppWidth_Mini(60));
    }];
    //联系方式
    [self.contentView addSubview:self.phoneLabel];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_right).offset(kAppWidth_Mini(30));
        make.top.height.equalTo(_nameLabel);
        make.right.mas_equalTo(-kAppWidth_Mini(50));
    }];
    //地址
    [self.contentView addSubview:self.addressLabel];
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_nameLabel.mas_bottom).offset(kAppWidth_Mini(10));
        make.right.mas_equalTo(-kAppWidth_Mini(50));
    }];
    //默认按钮
    [self.contentView addSubview:self.defaultButton];
    [_defaultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.mas_equalTo(kAppWidth_Mini(240));
        make.width.mas_equalTo(kAppWidth_Mini(250));
        make.height.mas_equalTo(kAppWidth_Mini(90));
    }];
    //编辑按钮
    [self.contentView addSubview:self.editButton];
    [_editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_defaultButton.mas_right).offset(kAppWidth_Mini(30));
        make.top.width.height.equalTo(_defaultButton);
    }];
    //删除按钮
    [self.contentView addSubview:self.deleteButton];
    [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_editButton.mas_right).offset(kAppWidth_Mini(30));
        make.top.width.height.equalTo(_defaultButton);
    }];
}

#pragma mark - 懒加载
-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFThinFont(13)];
    }
    return _nameLabel;
}

-(UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFThinFont(13)];
    }
    return _phoneLabel;
}

-(UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFThinFont(13)];
        _addressLabel.numberOfLines = 2;
    }
    return _addressLabel;
}

-(UIButton *)defaultButton{
    if (!_defaultButton) {
        _defaultButton = [UIButton initWithTitleColor:[UIColor blackColor] withFont:UIPFThinFont(13)];
        [_defaultButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _defaultButton.layer.borderWidth = 2;
    }
    return _defaultButton;
}

-(UIButton *)editButton{
    if (!_editButton) {
        _editButton = [UIButton initWithTitleStr:@"编辑" titleColor:[UIColor blackColor] withFont:UIPFThinFont(13)];
        [_editButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _editButton.layer.borderWidth = 2;
        _editButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }
    return _editButton;
}

-(UIButton *)deleteButton{
    if (!_deleteButton) {
        _deleteButton = [UIButton initWithTitleStr:@"删除" titleColor:[UIColor blackColor] withFont:UIPFThinFont(13)];
        [_deleteButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteButton.layer.borderWidth = 2;
        _deleteButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }
    return _deleteButton;
}

#pragma mark - 按钮点击事件
-(void)buttonAction:(UIButton *)button{
    if (self.cellButtonAction) {
        self.cellButtonAction(button.currentTitle);
    }
}

#pragma mark - 赋值模块
-(void)setListModel:(AddressListModel *)listModel{
    _listModel = listModel;
    _nameLabel.text = _listModel.consignee;
    _phoneLabel.text = _listModel.mobile;
    _addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",_listModel.provinceName,_listModel.cityName,_listModel.districtName,_listModel.address];
    if (_listModel.isdefault) {
        [_defaultButton setTitle:@"默认地址" forState:UIControlStateNormal];
        _defaultButton.layer.borderColor = [UIColor orangeColor].CGColor;
        [_defaultButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    }else{
         [_defaultButton setTitle:@"设为默认" forState:UIControlStateNormal];
        _defaultButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [_defaultButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}

@end
